package daily_exercise;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class twoDimensionalBag {

    public static int maxProfit(int[][] arr, int days){
        if (arr == null || arr.length == 0 || arr[0].length == 0){
            return 0;
        }
        int res = 0;
        List<List<Integer>> list = new ArrayList<>();
        for (int i = 0; i < arr.length; i++){
            List<Integer> temp = new ArrayList<>();
            temp.add(arr[i][0]);
            temp.add(arr[i][1]);
            list.add(temp);
        }
        Collections.sort(list, new com());
        // 成就越大排前面，

        int[][] dp = new int[arr.length][days + 1];
        for (int j = 0; j <= days; j++){
            dp[0][j] = j >= list.get(0).get(1) ? list.get(0).get(0) : 0;
        }
        for (int i = 1; i < arr.length; i++){
            for (int j = 1; j <= days; j++){
                if (j - list.get(i).get(1) < 0){ // 天数不够
                    dp[i][j] = dp[i-1][j];
                } else {
                    dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j-list.get(i).get(1)] + list.get(i).get(0));
                }
            }
        }
        return dp[arr.length - 1][days];
    }

    static class com implements Comparator<List<Integer>> {

        @Override
        public int compare(List<Integer> o1, List<Integer> o2) {
            if (o1.get(0).equals(o2.get(0))){
                return o1.get(1) - o2.get(1);
            }
            return o2.get(0) - o1.get(0);
        }
    }

    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s1 = br.readLine().split(" ");
        int num = Integer.parseInt(s1[0]);
        int days = Integer.parseInt(s1[1]);
        int[][] arr = new int[num][2];
        for (int i = 0; i < num; i++){
            String[] s2 = br.readLine().split(" ");
            arr[i][0] = Integer.parseInt(s2[0]);
            arr[i][1] = Integer.parseInt(s2[1]);
        }
        System.out.println(maxProfit(arr, days));
    }
}
